//-----------------------------------------------------------------------
// <copyright file="UserDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BirthdayReminder.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace BirthdayReminder.Data
{    
    public abstract class UserDAOBase
    {
        #region Common methods
        public virtual User CreateUserFromReader(IDataReader reader)
        {
            User item = new User();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("UserId"))) item.UserId = (int)reader["UserId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Email"))) item.Email = (string)reader["Email"];
                if (!reader.IsDBNull(reader.GetOrdinal("Cash"))) item.Cash = (int)reader["Cash"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreationDate"))) item.CreationDate = (DateTime)reader["CreationDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("LastLogin"))) item.LastLogin = (DateTime)reader["LastLogin"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateUserFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateUser methods
            
        public virtual void CreateUser(User user)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersInsert");
                
                database.AddInParameter(dbCommand, "@Email", DbType.String, user.Email);
                database.AddInParameter(dbCommand, "@Cash", DbType.Int32, user.Cash);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, user.CreationDate);
                database.AddInParameter(dbCommand, "@LastLogin", DbType.DateTime, user.LastLogin);
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, user.UserId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                //user.UserId = (int)database.GetParameterValue(dbCommand, "@UserId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateUserException, ex);
            }
        }

        #endregion
		
        #region UpdateUser methods
        
        public virtual void UpdateUser(User user)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersUpdate");            
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, user.UserId);
                database.AddInParameter(dbCommand, "@Email", DbType.String, user.Email);
                database.AddInParameter(dbCommand, "@Cash", DbType.Int32, user.Cash);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, user.CreationDate);
                database.AddInParameter(dbCommand, "@LastLogin", DbType.DateTime, user.LastLogin);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateUserException, ex);
            }
        }
        
        #endregion
		
        #region DeleteUser methods
        public virtual void DeleteUser(int userId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersDelete");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, userId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteUserException, ex);
            }
        }

        #endregion
		
        #region GetUser methods
        
        public virtual User GetUser(int userId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersGet");
                
                database.AddInParameter(dbCommand, "@UserId", DbType.Int32, userId);
                
                User user =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        user = CreateUserFromReader(reader);
                        reader.Close();
                    }
                }
                return user;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetUserException, ex);
            }
        }
        
        #endregion
		
        #region GetUserList methods
        public virtual UserCollection GetUserList(UserColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spUsersGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                UserCollection userCollection = new UserCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        User user = CreateUserFromReader(reader);
                        userCollection.Add(user);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return userCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetUserListException, ex);
            }
        }
        
        public virtual UserCollection GetUserList(UserColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetUserList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}